package com.orange.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthSchoolContactService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.manage.AuthOrgUser;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.modules.entity.school.AuthSchoolContacts;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/addContacts")
public class AuthAddContactsController extends BaseController {

	private static final Logger log = Logger.getLogger(AuthSchoolContactsController.class);

	@Autowired
	private AuthSchoolContactService authSchoolContactService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	/**
	 * 管理员添加学校联系人
	 */
	@RequestMapping(value = "/addSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addSchoolContacts(HttpServletRequest request ,@RequestBody Map<String ,String> map){
		try{
			AuthOrgUser pid = (AuthOrgUser) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null == pid)
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			//String schoolId="10054";
			AuthSchool a = authSchoolService.queryAuthSchoolAllId();
			if(a == null)
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"机构信息不存在");
			String schoolId = a.getPid();
			String name = map.get("name");
			String type = map .get("type");
			String mobilePhone = map.get("mobilePhone");
			String telephone = map.get("telephone");
			String email = map.get("email");
			if(StringUtils.isBlank(name))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			if(StringUtils.isBlank(type))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			if(StringUtils.isBlank(mobilePhone))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			if(StringUtils.isBlank(telephone))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			if(StringUtils.isBlank(email))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			AuthSchoolContacts as = new AuthSchoolContacts();
			as.setSchoolId(schoolId);
			as.setName(name);
			as.setType(type);
			as.setMobilePhone(mobilePhone);
			as.setTelephone(telephone);
			as.setEmail(email);
			as.setCreateTime(nowTime);
			authSchoolContactService.insertAuthSchoolContacts(as);
			return toResponse(null,ReturnCode.SUCCESS,"添加学校联系人成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.SUCCESS,"添加学校联系人失败");
		}
	}
	
	/**
	 * 管理员删除学校联系人
	 */
	@RequestMapping(value = "/deleteSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteSchoolContacts(HttpServletRequest request ,@RequestBody Map<String ,String> map){
		try{
			AuthOrgUser pid = (AuthOrgUser) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null == pid)
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			String pid1 = map.get("pid1");
			if(StringUtils.isBlank(pid1) || pid1 == null)
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"pid不能为空!");
			authSchoolContactService.deleteAuthSchoolContactsBypid(pid1);
			return toResponse(null,ReturnCode.SUCCESS,"删除成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"删除失败");	
		}
	}

	/**
	 * 管理员修改联系人信息
	 */
	@RequestMapping(value = "/updateSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateSchoolContacts(HttpServletRequest request ,@RequestBody Map<String ,String> map){
		try{
			AuthOrgUser pid = (AuthOrgUser) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null == pid)
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			String pid1 = map.get("pid1");
			String schoolId = map.get("schoolId");
			String name = map.get("name");
			String type = map.get("type");
			String mobilePhone = map.get("mobilePhone");
			String telephone = map.get("telephone");
			String email = map.get("email");
			AuthSchoolContacts ac = authSchoolContactService.queryAuthSchoolContactsBypid(pid1);
			if(ac == null){
				return toResponse(ac,ReturnCode.FAIL,"无该条数据");
			}
			if(schoolId!=null && StringUtils.isNotBlank(schoolId))
				ac.setSchoolId(schoolId);
			if(name!=null && StringUtils.isNotBlank(name))
				ac.setName(name);
			if(type!=null && StringUtils.isNotBlank(type))
				ac.setType(type);
			if(mobilePhone!=null && StringUtils.isNotBlank(mobilePhone))
				ac.setMobilePhone(mobilePhone);
			if(telephone!=null && StringUtils.isNotBlank(telephone))
				ac.setTelephone(telephone);
			if(email!=null && StringUtils.isNotBlank(email))
				ac.setEmail(email);
			authSchoolContactService.updateAuthSchoolContacts(ac);
			return toResponse(ac,ReturnCode.SUCCESS,"修改成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
	}
}
